// +build ignore

package main

import (
	"fmt"
	"os"
	"time"

	"gopl/goroutine/thumbnail"
)

func makeThumbnails(filenames []string) (thumbfiles []string, err error) {
	type item struct {
		thumbfile string
		err       error
	}
	ch := make(chan item, len(filenames))
	for _, f := range filenames {
		go func(f string) {
			var it item
			it.thumbfile, it.err = thumbnail.ImageFile(f)
			ch <- it
		}(f)
	}

	for range filenames {
		it := <-ch
		if it.err != nil {
			return nil, err
		}
		thumbfiles = append(thumbfiles, it.thumbfile)
	}
	return thumbfiles, nil
}

func elapse() func() {
	now := time.Now()
	return func() {
		fmt.Printf("elapse:%.3f ms\n", 1000*time.Since(now).Seconds())
	}
}

func main() {
	defer elapse()()
	_, err := makeThumbnails(os.Args[1:])
	if err != nil {
		fmt.Println(err)
	}
}
